package com.example.controller;

import com.example.entity.Goods;
import com.example.entity.Goodstype;
import com.example.service.GoodstypeService;
import com.example.service.impl.GoodServiceImpl;
import com.example.service.impl.GoodstypeServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
@Api(description = "这是商品控制器")

@Controller
@RequestMapping(value = "/admin")
public class Goodsctrl {
    @Resource
    GoodServiceImpl goodService;
    @Resource
    GoodstypeService goodstypeService;

    //    获取商品列表
    @ApiOperation(value = "获得所有商品",notes = "获得所有商品")
    @RequestMapping(value = "/goods",method = {RequestMethod.GET,RequestMethod.POST})
    public String GetGoodslist(Model model,
                               @RequestParam(value = "pageNum",required = false,defaultValue = "2")Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "3")Integer pageSize){
        PageInfo<Goods> goodsPageInfo=goodService.queryAll(pageNum,pageSize);
        model.addAttribute("goodsPageInfo",goodsPageInfo);
        return "goodslist";
    }
//    删除商品
    @ApiOperation("根据id删除商品")
    @GetMapping("/delGoods/{id}")
    public String delGoods(@PathVariable("id")int id,Model model){
        int s=goodService.deletebyid(id);
        model.addAttribute("zc","删除成功");
        return "forward:/admin/goods";
    }
//    获取商品类别
    @GetMapping("/goods/{id}")
    public String getGoodsById(@PathVariable("id") int id,Model model){
        Goods goods = goodService.findbyid(id);
        model.addAttribute("goods",goods);
        ArrayList<Goodstype> goodstypesList = goodstypeService.findAll();
        model.addAttribute("gdt",goodstypesList);
        return "dogoodslist";
    }
//    更新商品

    @PostMapping("/editGoods")
    public String editGoods(Goods goods,Model model){
        int s= goodService.updatebyid(goods);
        model.addAttribute("zc","更新成功");
        return "forward:/admin/goods";
    }
    @GetMapping("/insert")
    public String insert(Model model) {
        ArrayList<Goodstype> goodstypesList = goodstypeService.findAll();
        model.addAttribute("gdt", goodstypesList);
        return "ingoodslist";
    }
    @PostMapping ("/insertGoods")
    public String insertGoods(Goods goods,Model model){
        int d=goodService.insert(goods);
        return "forward:/admin/goods";
    }
    }
